import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:sainbainu/common/values/colors.dart';
import 'package:sainbainu/common/widgets/widget.dart';

/// 头像尺寸
enum AvatarSize {
  large,
  small,
}

/// 获取头像尺寸数值
double getAvatarSize(AvatarSize avatarSize) {
  double size = 50.0;
  switch (avatarSize) {
    case AvatarSize.large:
      size = 50.0;
      break;
    case AvatarSize.small:
      size = 44.0;
      break;
  }
  return size;
}

/// 头像昵称等组件
class UserInfoCell extends StatelessWidget {
  final String name;
  final String? avatar;
  final AvatarSize avatarSize;
  final String? role;
  final bool showNameAuth;
  final bool showRoleAuth;
  final String? nameAuthText;
  final double nameFontSize;
  final double roleFontSize;

  final String? groupName;
  final bool isBottomGroup;
  final String? region;
  final String? dateTime;

  final Widget? bottomChild;
  final Widget? rightChild;

  final String? avatarHeroTag;

  final VoidCallback? onUserTap;
  final VoidCallback? onGroupTap;

  const UserInfoCell({
    Key? key,
    this.avatar,
    required this.name,
    this.role,
    this.showNameAuth = false,
    this.showRoleAuth = false,
    this.nameAuthText,
    this.nameFontSize = 16.0,
    this.roleFontSize = 14.0,
    this.groupName,
    this.region,
    this.dateTime,
    this.avatarSize = AvatarSize.large,
    this.bottomChild,
    this.rightChild,
    this.avatarHeroTag,
    this.onUserTap,
    this.onGroupTap,
    this.isBottomGroup = false,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        _buildAvatar(onTap: onUserTap),
        const SizedBox(width: 10),
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _buildNameInfo(),
              if (bottomChild != null) bottomChild!,
            ],
          ),
        ),
      ],
    );
  }

  Widget _buildNameInfo() {
    final double size = getAvatarSize(avatarSize);
    return SizedBox(
      height: size,
      child: Row(
        children: [
          Expanded(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    _buildAuthName(onTap: onUserTap),
                    if (groupName != null &&
                        groupName!.isNotEmpty &&
                        !isBottomGroup)
                      _buildIconGroup(),
                  ],
                ),
                const SizedBox(height: 2),
                Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    if (role != null && role!.isNotEmpty || showRoleAuth)
                      _buildAuthRole(onTap: onUserTap),
                    Text.rich(TextSpan(
                      children: [
                        if (region != null && region!.isNotEmpty)
                          // TextSpan(text: 'IP ${'属地'.tr}：$region'),
                          TextSpan(text: region),
                        if (region != null &&
                            region!.isNotEmpty &&
                            dateTime != null)
                          const TextSpan(text: ' | '),
                        if (dateTime != null) TextSpan(text: dateTime),
                      ],
                      style: const TextStyle(
                        color: AppColors.unactive3,
                        fontSize: 13,
                      ),
                    )),
                    if (isBottomGroup &&
                        groupName != null &&
                        groupName!.isNotEmpty)
                      GestureDetector(
                        onTap: onGroupTap,
                        child: Padding(
                          padding: const EdgeInsets.only(left: 5),
                          child: Row(
                            children: [
                              Padding(
                                padding: const EdgeInsets.only(right: 2),
                                child: Image.asset(
                                  'assets/images/group-3.png',
                                  width: 14,
                                  height: 14,
                                ),
                              ),
                              Text(
                                groupName!,
                                style: const TextStyle(
                                  color: AppColors.primary,
                                  fontSize: 12,
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                  ],
                ),
              ],
            ),
          ),
          if (rightChild != null) rightChild!,
        ],
      ),
    );
  }

  /// 认证+角色
  Widget _buildAuthRole({
    VoidCallback? onTap,
  }) {
    return Expanded(
      child: GestureDetector(
        onTap: onTap,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            if (showRoleAuth)
              Padding(
                padding: const EdgeInsets.only(right: 2),
                child: Image.asset(
                  'assets/images/auth.png',
                  width: 14,
                  height: 14,
                ),
              ),
            if (role != null && role!.isNotEmpty)
              Expanded(
                child: Text(
                  role!,
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                    color: AppColors.unactive3,
                    fontSize: roleFontSize,
                  ),
                  strutStyle: const StrutStyle(
                    forceStrutHeight: true,
                  ),
                ),
              ),
          ],
        ),
      ),
    );
  }

  /// 小组名称
  Widget _buildIconGroup() {
    return GestureDetector(
      onTap: onGroupTap,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Padding(
            padding: const EdgeInsets.only(right: 2),
            child: Image.asset(
              'assets/images/group-3.png',
              width: 15,
              height: 15,
            ),
          ),
          // Text.rich(
          //   TextSpan(
          //     children: [
          //       TextSpan(
          //         text: '小组'.tr
          //       ),
          //       TextSpan(
          //         text: '：$groupName'
          //       ),
          //     ],
          //   ),
          //   style: const TextStyle(
          //     color: AppColors.active2,
          //     fontSize: 12,
          //   ),
          // ),
          Text(
            '${'小组'.tr}：$groupName',
            style: const TextStyle(
              color: AppColors.active2,
              fontSize: 12,
            ),
          ),
        ],
      ),
    );
  }

  /// 认证+昵称
  Widget _buildAuthName({
    VoidCallback? onTap,
  }) {
    return Expanded(
      child: GestureDetector(
        onTap: onTap,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            showNameAuth
                ? Padding(
                    padding: const EdgeInsets.only(right: 2),
                    child: Image.asset(
                      'assets/images/auth.png',
                      width: 14,
                      height: 14,
                    ),
                  )
                : const SizedBox.shrink(),
            Text(
              name,
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                color: const Color(0xff101010),
                fontSize: nameFontSize,
                fontWeight: FontWeight.w500,
              ),
            ),
            showNameAuth && nameAuthText != null
                ? Expanded(
                    child: Text(
                      ' - ${nameAuthText!}',
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                        color: AppColors.unactive3,
                        fontSize: roleFontSize,
                      ),
                    ),
                  )
                : const SizedBox.shrink(),
          ],
        ),
      ),
    );
  }

  /// 头像
  Widget _buildAvatar({
    VoidCallback? onTap,
  }) {
    final double size = getAvatarSize(avatarSize);

    Widget avatarWidget = GestureDetector(
      onTap: onTap,
      child: SizedBox(
        width: size,
        height: size,
        child: ClipRRect(
          borderRadius: BorderRadius.circular(size),
          child: CustomImage(
            url: avatar ?? '',
            errorType: ImageErrorType.avatar,
          ),
        ),
      ),
    );

    if (avatarHeroTag != null) {
      return Hero(
        tag: avatarHeroTag!,
        child: avatarWidget,
      );
    }
    return avatarWidget;
  }
}
